package com.wontube.smr.web.controller.admin;

import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.extension.utils.DateUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.web.client.admin.IAdminFeedbackHandler;
import com.wontube.smr.web.client.model.po.FeedbackReply;
import com.wontube.smr.web.client.model.vo.FeedbackItems;

/**
 * @author tanggf(2012-6-6)
 *
 */
@Controller
@RequestMapping("/admin/feedback")
public class AdminFeedbackController {
	private final static String SUCCESS = "Done";
	private final static String FAILURE = "Failure: something wrong";
	private final static int PAGE_SIZE = 10;

	@RequestMapping("")
	public ModelAndView feedback_home(){
		return new ModelAndView("admin/feedback/feedback_index");
	}
	
	@RequestMapping("/list")
	public ModelAndView list_feedback(
			@RequestParam(value="page", required=false, defaultValue="1") Integer page,
			@RequestParam("status") Integer status, 
			@RequestParam("feedType") Integer feedType){
		IAdminFeedbackHandler handler = getHandler();
		FeedbackItems items = handler.queryFeedbacks(status, feedType, new PagenateInfo(page, PAGE_SIZE));
		ModelAndView mv = new ModelAndView("admin/feedback/feedback_list");
		mv.addObject("feedbackItems", items);
		return mv;
	}
	
	@RequestMapping(value="/delete/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String deleteFeedback(@PathVariable("id") String id){
		IAdminFeedbackHandler handler = getHandler();
		Integer aff = handler.deleteFeedbacks(new String[]{id});
		if(aff !=null && aff > 0){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping(value="/undelete/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String undeleteFeedback(@PathVariable("id") String id){
		IAdminFeedbackHandler handler = getHandler();
		Integer aff = handler.undeleteFeedback(id);
		if(aff !=null && aff > 0){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping(value="/reply", method=RequestMethod.POST)
	@ResponseBody
	public String add_feedback_reply(FeedbackReply reply){
		if(StringUtils.isNullOrEmpty(reply.getContent())){
			return "Reply content is empty";
		}
		reply.setId(UUID.randomUUID().toString());
		reply.setCreated(DateUtils.getNow());
		IAdminFeedbackHandler handler = getHandler();
		int aff = handler.replyFeedback(reply);
		if(aff == 1){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping(value="/reply/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete_feedback_reply(String id){
		if(id == null){
			return "Id can't null!";
		}
		IAdminFeedbackHandler handler = getHandler();
		int result = handler.deleteFeedbackReply(id);
		if(result > 0){
			return "success";
		}
		return "fail";
	}
	
	private IAdminFeedbackHandler getHandler(){
		Remoting<IAdminFeedbackHandler> remoting = new Remoting<IAdminFeedbackHandler>();
		return remoting.createService(IAdminFeedbackHandler.class);
	}
}
